home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / WIN_PRO / DS-1.ZIP;1 / RUNTIME.ZIP / RSYS.R < prev   
Encoding:
Text File  |  1992-02-10  |  5.9 KB  |  271 lines

  1. /*
  2.  * File: rsys.r
  3.  *  Contents: [flushrec], [getrec], getstrg, host, longread, [putrec], putstr
  4.  */
  5.  
  6. #ifdef RecordIO
  7. /*
  8.  * flushrec - force buffered output to be written with a record break.
  9.  *  Applies only to files with mode "s".
  10.  */
  11.  
  12. novalue flushrec(fd)
  13. FILE *fd;
  14. {
  15. #if SASC
  16.    afwrite("", 1, 0, fd);
  17. #endif                    /* SASC */
  18. }
  19.  
  20. /*
  21.  * getrec - read a record into buf from file fd. At most maxi characters
  22.  *  are read.  getrec returns the length of the record.
  23.  *  Returns -1 if EOF and -2 if length was limited by
  24.  *  maxi. [[ Needs ferror() check. ]]
  25.  *  This function is meaningful only for files opened with mode "s".
  26.  */
  27.  
  28. int getrec(buf, maxi, fd)
  29. register char *buf;
  30. int maxi;
  31. FILE *fd;
  32.    {
  33. #ifdef SASC
  34.    register int l;
  35.  
  36.    l = afreadh(buf, 1, maxi+1, fd);     /* read record or maxi+1 chars */
  37.    if (l == 0) return -1;
  38.    if (l <= maxi) return l;
  39.    ungetc(buf[maxi], fd);               /* if record not used up, push
  40.                                            back last char read */
  41.    return -2;
  42. #endif                    /* SASC */
  43.    }
  44. #endif                    /* RecordIO */
  45.  
  46. /*
  47.  * getstrg - read a line into buf from file fd.  At most maxi characters
  48.  *  are read.  getstrg returns the length of the line, not counting
  49.  *  the newline.  Returns -1 if EOF and -2 if length was limited by
  50.  *  maxi. [[ Needs ferror() check. ]]
  51.  */
  52.  
  53. int getstrg(buf, maxi, fd)
  54. register char *buf;
  55. int maxi;
  56. FILE *fd;
  57.    {
  58.    register int c, l;
  59.  
  60.  
  61. #if AMIGA
  62. #if LATTICE
  63.    /* This code is special for Lattice 4.0.  It was different for
  64.     *  Lattice 3.10 and probably won't work for other C compilers.
  65.     */
  66.    extern struct UFB _ufbs[];
  67.  
  68.    if (IsInteractive(_ufbs[fileno(fd)].ufbfh))
  69.       return read(fileno(fd),buf,maxi);
  70. #endif                    /* LATTICE */
  71. #endif                    /* AMIGA */
  72.  
  73.  
  74.    l = 0;
  75.    while (1) {
  76.       if ((c = fgetc(fd)) == '\n') break;
  77.       if (c == EOF)
  78.      if (l > 0) return l;
  79.      else return -1;
  80.       if (++l > maxi) {
  81.      ungetc(c, fd);
  82.      return -2;
  83.      }
  84.       *buf++ = c;
  85.       }
  86.    return l;
  87.    }
  88.  
  89. /*
  90.  * iconhost - return some sort of host name into the buffer pointed at
  91.  *  by hostname.  This code accommodates several different host name
  92.  *  fetching schemes.
  93.  */
  94. novalue iconhost(hostname)
  95. char *hostname;
  96.    {
  97.  
  98. #ifdef WhoHost
  99.    /*
  100.     * The host name is in /usr/include/whoami.h. (V7, 4.[01]bsd)
  101.     */
  102.    whohost(hostname);
  103. #endif                    /* WhoHost */
  104.  
  105. #ifdef UtsName
  106.    {
  107.    /*
  108.     * Use the uname system call.  (System III & V)
  109.     */
  110.    struct utsname utsn;
  111.    uname(&utsn);
  112.    strcpy(hostname,utsn.nodename);
  113.    }
  114. #endif                    /* UtsName */
  115.  
  116. #ifdef GetHost
  117.    /*
  118.     * Use the gethostname system call.  (4.2bsd)
  119.     */
  120.    gethostname(hostname,MaxCvtLen);
  121. #endif                    /* GetHost */
  122.  
  123. #if VMS
  124.    /*
  125.     * VMS has its own special logic.
  126.     */
  127.    char *h;
  128.    if (!(h = getenv("ICON_HOST")) && !(h = getenv("SYS$NODE")))
  129.       h = "VAX/VMS";
  130.    strcpy(hostname,h);
  131. #endif                    /* VMS */
  132.  
  133. #ifdef HostStr
  134.    /*
  135.     * The string constant HostStr contains the host name.
  136.     */
  137.    strcpy(hostname,HostStr);
  138. #endif                    /* HostStr */
  139.  
  140.    }
  141.  
  142. #ifdef WhoHost
  143. #define HdrFile "/usr/include/whoami.h"
  144.  
  145. /*
  146.  * whohost - look for a line of the form
  147.  *  #define sysname "name"
  148.  * in HdrFile and return the name.
  149.  */
  150. novalue whohost(hostname)
  151. char *hostname;
  152.    {
  153.    char buf[BUFSIZ];
  154.    FILE *fd;
  155.  
  156.    fd = fopen(HdrFile, ReadText);
  157.    if (fd == NULL) {
  158.       sprintf(buf, "Cannot open %s, no value for &host\n", HdrFile);
  159.       syserr(buf);
  160.    }
  161.  
  162.    for (;;) {   /* each line in the file */
  163.       if (fgets(buf, sizeof buf, fd) == NULL) {
  164.          sprintf(buf, "No #define for sysname in %s, no value for &host\n",
  165.             HdrFile);
  166.          syserr(buf);
  167.       }
  168.       if (sscanf(buf,"#define sysname \"%[^\"]\"", hostname) == 1) {
  169.          fclose(fd);
  170.          return;
  171.       }
  172.    }
  173.    }
  174. #endif                    /* WhoHost */
  175.  
  176. /*
  177.  * Read a long string in shorter parts. (Standard read may not handle long
  178.  *  strings.)
  179.  */
  180. word longread(s,width,len,fname)
  181. FILE *fname;
  182. int width;
  183. char *s;
  184. long len;
  185. {
  186.    long tally = 0;
  187.    long n = 0;
  188.  
  189.    while (len > 0) {
  190.       n = fread(s, width, (int)((len < MaxIn) ? len : MaxIn), fname);
  191.       if (n <= 0)
  192.          return tally;
  193.       tally += n;
  194.       s += n;
  195.       len -= n;
  196.       }  
  197.    return tally;
  198.    }
  199.  
  200. #ifdef RecordIO
  201. /*
  202.  * Write string referenced by descriptor d, avoiding a record break.
  203.  *  Applies only to files openend with mode "s".
  204.  */
  205.  
  206. int putrec(f, d)
  207. register FILE *f;
  208. dptr d;
  209.    {
  210. #if SASC
  211.    register char *s;
  212.    register word l;
  213.  
  214.    l = StrLen(*d);
  215.    if (l == 0)
  216.       return Succeeded;
  217.    s = StrLoc(*d);
  218.  
  219.    if (afwriteh(s,1,l,f) < l)
  220.       return Failed;
  221.    else
  222.       return Succeeded;
  223.    /*
  224.     * Note:  Because RecordIO depends on SASC, and because SASC
  225.     *  uses its own malloc rather than the Icon malloc, file usage
  226.     *  cannot cause a garbage collection.  This may require
  227.     *  reevaluation if RecordIO is supported for any other compiler.
  228.     */
  229. #endif                    /* SASC */
  230.    }
  231. #endif                    /* RecordIO */
  232.  
  233. /*
  234.  * Print string referenced by descriptor d. Note, d must not move during
  235.  *   a garbage collection.
  236.  */
  237.  
  238. int putstr(f, d)
  239. register FILE *f;
  240. dptr d;
  241.    {
  242.    register char *s;
  243.    register word l;
  244.  
  245.    l = StrLen(*d);
  246.    if (l == 0)
  247.       return  Succeeded;
  248.    s = StrLoc(*d);
  249.  
  250. #ifdef FixedRegions
  251.    if (longwrite(s,l,f) < 0)
  252.       return Failed;
  253.    else
  254.       return Succeeded;
  255. #else                    /* FixedRegions */
  256.    /*
  257.     * In expandable regions storage management, the first output to a file may
  258.     *  cause allocation, which in turn may cause a garbage collection, changing
  259.     *  where the string is.  So write one character and reload the address
  260.     *  of the string from the tended descriptor.
  261.     */
  262.  
  263.    putc(*s, f);
  264.    s = StrLoc(*d) + 1;
  265.    if (longwrite(s,--l,f) < 0)
  266.       return Failed;
  267.    else
  268.       return Succeeded;
  269. #endif                    /* FixedRegions */
  270.    }
  271.